package com.djx.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: HelloController
 * @Description: TODO
 * @author: 星~
 * @date: 2022-2-7 下午 14:26
 */
@Slf4j
@RestController
public class HelloController {


    //定义耗时操作
    private String doThing(String msg) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @GetMapping("/common")
    public String commonHandle() {
        log.info("common-start");
        //执行耗时操作
        String result = doThing("common handler");
        log.info("common-end");
        return result;
    }

    @GetMapping("/mono")
    public Mono<String> monoHandle() {
        log.info("mono-start");
        //执行耗时操作
        Mono<String> mono = Mono.fromSupplier(() -> doThing("mono handle"));
        log.info("mono-end");
        //Mono表示包含0或1个元素的异步序列
        return mono;
    }


    @RequestMapping("/array")
    public Flux<String> fluxHandle() {
        String[] cities = {"张三", "李四", "王五"};
        //Flux表示包含0-n个元素的异步序列
        Flux<String> stringFlux = Flux.fromArray(cities);
        return stringFlux;
    }

    public static void main(String[] args) {
        String a = "我丢%s%s";
        String format = String.format(a,"你好","狗贼");
        System.out.println(format);
    }

}
